/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 *
 */
package org.apache.mina.coap.resource;

import static org.junit.Assert.*;

import org.apache.mina.api.IoSession;
import org.apache.mina.coap.CoapCode;
import org.apache.mina.coap.CoapMessage;
import org.apache.mina.coap.CoapOption;
import org.apache.mina.coap.CoapOptionType;
import org.apache.mina.coap.MessageType;
import org.junit.Test;

/**
 * 
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
public class ResourceRegistryTest {

    @Test
    public void generate_4_04_on_unknown_resources() {
        ResourceRegistry reg = new ResourceRegistry();
        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "test".getBytes()) }, new byte[] {});
        CoapMessage resp = reg.respond(msg, null);
        assertEquals(1, resp.getVersion());
        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());
        assertArrayEquals(new CoapOption[] { new CoapOption(CoapOptionType.CONTENT_FORMAT, new byte[] { 0 }) },
                resp.getOptions());
        assertArrayEquals("not found !".getBytes(), resp.getPayload());
    }

    @Test
    public void call_resource_handler() {
        ResourceRegistry reg = new ResourceRegistry();
        ResourceHandler handler = new AbstractResourceHandler() {

            @Override
            public CoapResponse handle(CoapMessage request, IoSession session) {
                return new CoapResponse(12345, "bla bla".getBytes(), new CoapOption[] {});
            }

            @Override
            public String getPath() {
                return "myTest/Path";
            }
        };
        reg.register(handler);

        // 4.04 on bad path
        CoapMessage msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null,
                new CoapOption[] { new CoapOption(CoapOptionType.URI_PATH, "meh".getBytes()) }, new byte[] {});
        CoapMessage resp = reg.respond(msg, null);
        assertEquals(CoapCode.NOT_FOUND.getCode(), resp.getCode());

        // on correct path the 2.02 with the response
        msg = new CoapMessage(1, MessageType.CONFIRMABLE, CoapCode.GET.getCode(), 123, null, new CoapOption[] {
                                new CoapOption(CoapOptionType.URI_PATH, "myTest".getBytes()),
                                new CoapOption(CoapOptionType.URI_PATH, "Path".getBytes()) }, new byte[] {});
        resp = reg.respond(msg, null);
        assertEquals(1, resp.getVersion());
        assertEquals(12345, resp.getCode());
        assertArrayEquals(new CoapOption[] {}, resp.getOptions());
        assertArrayEquals("bla bla".getBytes(), resp.getPayload());
    }
}